package com.kehutong.payment.service;

import com.kehutong.payment.dto.OrderRefundReqDto;
import com.kehutong.payment.dto.OrderRefundResDto;
import com.kehutong.payment.entity.OrderRefund;
import com.kehutong.payment.entity.ThirdOrder;
import com.kehutong.payment.enums.RefundStatusEnum;
import com.kehutong.payment.util.UUIDUtils;
import org.coraframework.authz.SubjectUtil;
import org.coraframework.inject.Inject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import java.time.LocalDateTime;

/**
 * 抽象退款接口
 *
 * @author wupengfei
 */
public abstract class AbstractRefundService implements RefundService {

    private static final Logger logger = LoggerFactory.getLogger(AbstractRefundService.class);

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Object refundOrder(OrderRefundReqDto orderRefundDto) {
        logger.info("订单退款orderRefundDto=" + orderRefundDto);

        // 查询订单
        ThirdOrder thirdOrder = jdbcSession.findById(ThirdOrder.class, orderRefundDto.getThirdOrderId());
        if (thirdOrder == null) {
            logger.info("订单id不存在");
            return Result.badArgument();
        }

        // 校验是否已退款
        if (thirdOrder.getRefundStatus() != RefundStatusEnum.NOT_REFUND.getValue()) {
            logger.info("订单已退款");
            return Result.badArgument();
        }

        // 新增退款记录
        String uuid = UUIDUtils.getUuid();
        OrderRefund orderRefund = Pool.newInstance(OrderRefund.class, uuid);
        orderRefund.setThirdOrderId(orderRefundDto.getThirdOrderId());
        orderRefund.setTradeNo(uuid);
        orderRefund.setRefundDate(LocalDateTime.now());
        orderRefund.setIsSuccess(0);
        orderRefund.setRefundMoney(thirdOrder.getOrderFee());
        orderRefund.setRefundReason(orderRefundDto.getRefundReason());
//        orderRefund.setCreateUserId(SubjectUtil.getSubject().getUserId());
        jdbcSession.insert(orderRefund);

        // 第三方退款
        OrderRefundResDto result = null;
        try {
            result = doRefundOrder(thirdOrder, orderRefund);
            logger.info("退款结果orderRefundResDto=" + result);
            if (result.isSuccess()) {
                orderRefund.setIsSuccess(1);
                orderRefund.setVendorTradeNo(result.getVendorTradeNo());
                jdbcSession.updateById(orderRefund);

                thirdOrder.setRefundStatus(RefundStatusEnum.ALL_REFUND.getValue());
                jdbcSession.updateById(thirdOrder);
            }
            return result;
        } catch (Exception e) {
            logger.error("退款时异常", e);
            return Result.error(315008, result != null ? result.getDescription() : "退款失败");
        }
    }

    /**
     * 第三方退款
     *
     * @param thirdOrder  订单记录
     * @param orderRefund 退款记录
     * @return 退款对象
     */
    public abstract OrderRefundResDto doRefundOrder(ThirdOrder thirdOrder, OrderRefund orderRefund) throws Exception;

}
